<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { ElButton } from 'element-plus'

import { Table } from '@/components/Table'

// import { zcPastureListApi } from '@/api/fa/pasture'
import { useTable } from '@/hooks/web/useTableTest'
import { reactive, ref } from 'vue'

import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { dbPastureListApi, dbPastureexportApi } from '@/api/forms'
import { ucOrgListApi } from '@/api/uc/org'
import { ucUserListAdminApi } from '@/api/uc/user'
// import { useCache } from '@/hooks/web/useCache'
// const { wsCache } = useCache()

const { register, tableObject, methods } = useTable<
  {
    total: number
    list: any[]
  },
  any
>({
  getListApi: dbPastureListApi,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { setSearchParams } = methods

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'id',
    label: '主键',
    fixed: 'left',
    width: 60,
    table: {
      show: false
    },
    form: {
      show: false
    },
    detail: {
      show: false
    }
  },
  {
    field: 'pasture_org_id',
    label: '所属组织',
    search: {
      component: 'DialogRefer',
      componentProps: {
        getListApi: ucOrgListApi,
        response: {
          count: 'count',
          data: 'data'
        },
        title: '选择组织（双击选择）',
        nameField: 'org_name',
        valueField: 'org_id',
        crudSchemas: [
          {
            field: 'org_name',
            label: '组织名称',
            fixed: 'left',
            search: {
              show: true
            }
          }
        ]
      },
      show: true
    },
    form: {
      component: 'DialogRefer',
      componentProps: {
        getListApi: ucOrgListApi,
        response: {
          count: 'count',
          data: 'data'
        },
        title: '选择组织（双击选择）',
        nameField: 'org_name',
        valueField: 'org_id',
        crudSchemas: [
          {
            field: 'org_name',
            label: '组织名称',
            fixed: 'left',
            search: {
              show: true
            }
          }
        ]
      },
      show: true
    },
    table: {
      show: false
    },
    detail: {
      show: false
    }
  },
  {
    field: 'org_name',
    label: '牧场名称',
    fixed: 'left',
    width: 200,
    search: {
      show: false
    }
  },
  {
    field: 'asset_count',
    label: '打标数',
    width: 150,
    detail: {
      span: 2
    }
  },
  {
    field: 'insure_asset_count',
    label: '抵押数',
    width: 150,
    detail: {
      span: 2
    }
  },
  {
    field: 'loan_asset_count',
    label: '投保数',
    width: 150,
    detail: {
      span: 2
    }
  },
  {
    field: 'legal_name',
    label: '法人姓名',
    width: 150,
    search: {
      show: false
    }
  },
  {
    field: 'unified_code',
    label: '统一社会信用代码',
    width: 150,
    search: {
      show: false
    }
  },
  {
    field: 'address',
    label: '牧场地址',
    width: 150,
    search: {
      show: true
    }
  },
  {
    field: 'owner_name',
    label: '牧场主姓名',
    width: 150,
    search: {
      show: true
    }
  },
  {
    field: 'phone',
    label: '联系电话',
    width: 150,
    search: {
      show: false
    }
  },
  {
    field: 'postal_code',
    label: '邮政编码',
    width: 150,
    table: {
      show: false
    }
  },
  {
    field: 'jurisdiction',
    label: '所属辖区',
    width: 150
  },
  {
    field: 'park_name',
    label: '关联园区',
    width: 150
  },
  {
    field: 'unified_code',
    label: '统一社会信用代码',
    width: 150
  },
  {
    field: 'business_license',
    label: '营业执照',
    width: 150,
    detail: {
      span: 2
    }
  },
  {
    field: 'update_user_id',
    label: '创建人',
    width: 80,
    search: {
      component: 'DialogRefer',
      componentProps: {
        getListApi: ucUserListAdminApi,
        response: {
          count: 'count',
          data: 'data'
        },
        title: '选择创建人（双击选择）',
        nameField: 'user_name',
        relNameField: 'nickname',
        valueField: 'user_id',
        crudSchemas: [
          {
            field: 'nickname',
            label: '姓名',
            search: {
              show: true
            }
          },
          {
            field: 'username',
            label: '用户名',
            search: {
              show: true
            }
          }
        ]
      },
      show: true
    },
    table: {
      show: false
    },
    detail: {
      show: false
    }
  },
  {
    field: 'create_user_name',
    label: '创建人',
    width: 150
  },
  {
    field: 'create_time',
    label: '创建时间',
    width: 170,
    form: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        disabled: true,
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    },
    search: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        'is-range': true,
        'unlink-panels': true,
        rangeSeparator: 'To',
        type: 'daterange',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const exportClick = async () => {
  // 弹出导出框
  dialogTableVisible.value = true
  // 获取查询条件
  let aaa: any = tableObject.paramsObj.params
  console.log(aaa)
  // 获取条数
  let { data }: any = await dbPastureexportApi({
    address: aaa.address,
    create_time: aaa.create_time,
    legal_name: aaa.legal_name,
    org_name: aaa.org_name
  })
    .catch(() => {})
    .finally(() => {
      // saveLoading.value = false
    })
  if (data) {
    // window.open(data.url)

    console.log(data.count, Math.ceil(data.count / 10000))
    FormsNumber.value = Math.ceil(data.count / 10000)
  }
}

// 下载次数
const FormsNumber = ref(0)
const dialogTableVisible = ref(false)

const Export = async (index) => {
  let aaa: any = tableObject.paramsObj.params
  let { data }: any = await dbPastureexportApi({
    address: aaa.address,
    create_time: aaa.create_time,
    legal_name: aaa.legal_name,
    org_name: aaa.org_name,
    pageNo: index,
    pageSize: 10000
  })
    .catch(() => {})
    .finally(() => {
      // saveLoading.value = false
    })
  if (data) {
    window.open(data.url)
  }
}
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
    <div class="mb-10px w-[100%] flex justify-end" v-show="tableObject.paramsObj.params">
      <ElButton type="primary" @click="exportClick()"> 导出EXCEL </ElButton>
    </div>
    <Table
      v-show="tableObject.paramsObj.params"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :border="true"
      :pagination="{
        total: tableObject.count
      }"
      @register="register"
      :selection="false"
    />
  </ContentWrap>

  <el-dialog v-model="dialogTableVisible" title="牧场主档导出详情">
    <div style="margin: 20px" v-for="(item, index) in FormsNumber" :key="index">
      <span style="margin: 20px">牧场主档 - {{ item }}</span>
      <ElButton type="primary" @click="Export(item)">导出</ElButton>
    </div>
  </el-dialog>
</template>
